package com.easyedu.core.manager;

import com.easyedu.core.dao.OpenCourseTimeDao;
import com.easyedu.core.dao.OpenCourseTimeMidDao;
import com.easyedu.core.entity.DbError;
import com.easyedu.core.entity.DeanServiceError;
import com.easyedu.core.entity.po.OpenCourseTime;
import com.easyedu.core.entity.po.OpenCourseTimeMid;
import com.easyedu.core.entity.po.OpenCourseTimeVersion;
import com.easyedu.core.exception.EasyEduException;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author linxi
 * @date 2022/5/14
 */

@Service
public class TimeTableManager {

     private final OpenCourseTimeDao openCourseTimeDao;

     private final OpenCourseTimeMidDao openCourseTimeMidDao;

     public TimeTableManager(OpenCourseTimeDao openCourseTimeDao, OpenCourseTimeMidDao openCourseTimeMidDao) {
          this.openCourseTimeDao = openCourseTimeDao;
          this.openCourseTimeMidDao = openCourseTimeMidDao;
     }

     @Transactional(rollbackFor = Exception.class)
     public void copyTimeTableFromMid(OpenCourseTimeVersion version) {


          //校验中间课表是否存在课程
          List<OpenCourseTimeMid> midTable =
                  openCourseTimeMidDao.getByVersionId(version.getOpenCourseTimeMidVersionId());
          if (midTable == null || midTable.size() == 0) {
               throw new EasyEduException(DeanServiceError.ERR_TIME_TABLE_MID_NOT_EXIST);
          }

          //校验课表否存在
          List<OpenCourseTime> timeTables =
                  openCourseTimeDao.getByOpenYearAndSemester(version.getOpenYear(), version.getSemester());
          if (timeTables == null || timeTables.size() > 0) {
               throw new EasyEduException(DeanServiceError.ERR_TIME_TABLE_EXIST);
          }
          //复制课表
          Integer integer = openCourseTimeDao.getTimeTableFromMid(version.getOpenCourseTimeMidVersionId());
//          if (integer == 0) {
//               throw new EasyEduException(DbError.Err_INSERT_DB_ERROR,"复制课表失败");
//          }
     }
}
